<?php

namespace App;

use Exception;
use Storage;
use DB;
use Illuminate\Database\Eloquent\Model;

class AdList extends Model
{
    protected $table = "kl_ad_list";
    public $timestamps = false;

    const STATUS_ON = 1;
    const STATUS_OFF = 2;

    static $StatusDisplay = [
        self::STATUS_ON => '未删除',
        self::STATUS_OFF => '已删除',
    ];


    const TYPE_START = 1;
    const TYPE_BANNER_HOME = 2;
    const TYPE_BANNER_KESHI = 3;
    const TYPE_POSTINGS_LIST = 4;
    const TYPE_COlUMN_STRIP = 5;
    const TYPE_COlUMN_HANGING = 6;
    const TYPE_ADVICE_BOTTOM = 7;

    static $TypeDisplay = [
        self::TYPE_START => '启动页广告',
        self::TYPE_BANNER_HOME => '首页banner图',
        self::TYPE_BANNER_KESHI => '科室栏目页banner图',
        self::TYPE_POSTINGS_LIST => '帖子列表中间广告条',
        self::TYPE_COlUMN_STRIP => '栏目内页广告条',
        self::TYPE_COlUMN_HANGING => '栏目内部商务通悬窗',
        self::TYPE_ADVICE_BOTTOM => '咨询文章底部广告位',
    ];


    public function adCompany()
    {
        return $this->belongsTo(AdCompanies::class, 'company_id', 'id');
    }


    /**
     * 新增
     * @param $request
     * @return array
     */
    static function AdPublish($request)
    {
        try {
            DB::beginTransaction();

            //添加该公司的广告条数
            $ad_num = DB::table('ad_companies')->where('id', $request->company_id)->increment('ad_num');
            $ad_on_num = DB::table('ad_companies')->where('id', $request->company_id)->increment('ad_on_num');
            if (!$ad_num || !$ad_on_num) {
                DB::rollBack();

                return error();
            }
            $ad = new AdList();
            foreach (['company_id', 'title', 'url_link', 'type', 'design_cost', 'time_cost', 'explain', 'image'] as $item) {
                $ad->$item = $request->$item;
            }
            $ad->department_id = join(',', $request->department_id);
            $ad->created_at = time();
            if (!$ad->save()) {
                DB::rollBack();

                return error();
            }
        } catch (Exception $exception) {
            DB::rollBack();

            logger($exception->getMessage());

            return error();
        }
        DB::commit();

        return success();
    }


    /**
     * 修改
     * @param $adList
     * @param $request
     * @return array
     */
    static function AdModify($adList, $request)
    {
        try {
            foreach (['company_id', 'title', 'url_link', 'type', 'design_cost', 'time_cost', 'explain', 'image'] as $item) {
                $adList->$item = $request->$item;
            }
            $department_id = array_filter($request->department_id);
            $adList->department_id = join(',', $department_id);
            if (!$adList->save()) {

                return error();
            }
        } catch (Exception $exception) {
            logger($exception->getMessage());

            return error();
        }

        return success();
    }

    static function AdDel($adList)
    {
        try {
            DB::beginTransaction();

            //减少该公司的广告条数
            $ad_on_num = DB::table('ad_companies')->where('id', $adList->company_id)->decrement('ad_on_num');
            if (!$ad_on_num) {
                DB::rollBack();

                return error();
            }
            $adList->status = self::STATUS_OFF;
            if (!$adList->save()) {
                DB::rollBack();

                return error();
            }
        } catch (Exception $exception) {
            DB::rollBack();

            logger($exception->getMessage());

            return error();
        }
        DB::commit();

        return success();
    }

}
